﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkflowWizart.Lib;
using WorkflowWizart.Lib.Entities;
using WorkflowWizart.Lib.Engine;
using WorkflowWizart.Lib.Utility.Costants;
using WorkflowWizart.Lib.Entities.WorkflowEntities;

namespace WorkflowWizart.WinTest
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            operations.DataSource = AppGlobals.OperationCommands;
            operations.DisplayMember = "DisplayName";
            operations.SelectedIndexChanged += new EventHandler(operations_SelectedIndexChanged);
        }

        void operations_SelectedIndexChanged(object sender, EventArgs e)
        {
            //var command = operations.SelectedItem as OperationCommand;
            //WorkflowItem item = new WorkflowItem();
            //item.DisplayName = "Deneme yapıoz";
            //item.Index = 0;
            //item.OperationCommandId = command.Id;
            //item.Parameters = new List<WorkflowItemParameter>();
            //item.Parameters.Add(new WorkflowItemParameter() { Value = "                   deneme  " });
            //item.ParentItem = null;
            //item.WorkflowItemType = Lib.Utility.Costants.WorkflowItemTypes.OperationCommand;
            //var result = WorkflowEngine.RunWorkFlowItem(item);
            //MessageBox.Show(result.ToString());


        }

        private void button1_Click(object sender, EventArgs e)
        {

            Workflow wf = new Workflow();
            wf.Name = "test";
            wf.Parameters = new List<WorkflowParameter>();
            wf.ReturnType = ReturnTypes.Value;
            wf.Items = new List<WorkflowItem>();
            wf.Parameters.Add(new WorkflowParameter() {  Name="browser", Type=WorkflowParameterTypes.Value, Value="hhhhhhhhh" });

            var command = operations.SelectedItem as OperationCommand;
            WorkflowItem item = new WorkflowItem();
            item.DisplayName = "1";
            item.Index = 0;
            item.OperationCommandId =command.Id;
            item.Parameters = new List<WorkflowItemParameter>();
            for (int i = 0; i < command.CommandParameters.Count; i++)
            { 
            item.Parameters.Add(new WorkflowItemParameter() { VariableName = "browser" });
            } 
            item.ParentItem = null; 
            item.ParentWorkflow = wf;
            wf.Items.Add(item);


            LoopWorkFlowItem loopItem = new LoopWorkFlowItem();
            loopItem.DisplayName = "2";
            loopItem.Index = 1;
            loopItem.Parameters = new List<WorkflowItemParameter>();
            loopItem.Parameters.Add(new WorkflowItemParameter() {  Value = "true"});
            loopItem.ParentItem = null; 
            loopItem.ParentWorkflow = wf;
            wf.Items.Add(loopItem);

            WorkflowItem loopChild = new WorkflowItem();
            loopChild.DisplayName = "3";
            loopChild.Index = 2;
            loopChild.Parameters = new List<WorkflowItemParameter>();
            loopChild.Parameters.Add(new WorkflowItemParameter() { VariableName = loopChild.Id + ":Result" });
            loopChild.Parameters.Add(new WorkflowItemParameter() { VariableName = item.Id + ":Result" });
            loopChild.OperationCommandId = AppGlobals.OperationCommands.SingleOrDefault(o => o.Name == "Add").Id;
            loopChild.ParentItem = loopItem; 
            loopChild.ParentWorkflow = wf;
            wf.Items.Add(loopChild);
            loopItem.AddChildItem(loopChild);

            WorkflowItem loopConditionChild = new WorkflowItem();
            loopConditionChild.DisplayName = "4";
            loopConditionChild.Index = 3;
            loopConditionChild.Parameters = new List<WorkflowItemParameter>();
            loopConditionChild.Parameters.Add(new WorkflowItemParameter() {  Value="true" }); 
            loopConditionChild.ParentItem = loopItem; 
            loopConditionChild.ParentWorkflow = wf;
            wf.Items.Add(loopConditionChild); 
            loopItem.AddConditionItem(loopConditionChild);
              

            ReturnWorkFlowItem returnItem = new ReturnWorkFlowItem();
            returnItem.DisplayName = "5";
            returnItem.Index = 1;
            returnItem.Parameters = new List<WorkflowItemParameter>();
            returnItem.Parameters.Add(new WorkflowItemParameter() { VariableName = loopChild.Id + ":Result" });
            returnItem.ParentItem = null; 
            returnItem.ParentWorkflow = wf;
             

            wf.Items.Add(returnItem);

            var result = WorkflowEngine.RunWorkflow(wf);
            MessageBox.Show(result.ToString().Substring(0,1000));

        }
    }
}
